<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-03-28
 * Time: 下午 4:26
 */
namespace App;
defined('IN_IA') or exit('Access Denied');
use Endroid\QrCode\QrCode;
use Model\LinkMod;
use Model\RedMod;
use Model\WqFansMod;
use Model\WqMemberMod;
use Pub\cache\Cache;

class Action
{
    protected $_data = array();
    protected $_result = array('status'=>'error');
    protected $_gpc = array();
    protected $_w = array();
    protected $_uniacid = 0; //公众号id
    protected $_uid = 0;


    private $WqMember = null;  //微擎会员
    private $WqFans = null; //微擎粉丝
    private $Red = null;  //红包
    private $Link = null;  //红包的推广链接

    protected function __construct()
    {
        global $_GPC,$_W;
        $this->_gpc = $_GPC;
        $this->_w = $_W;
        $this->_uniacid = $_W["account"]["uniacid"];
    }

    /**
     * 接口返回内容处理
     * @param $_data
     */
    protected function result($_data)
    {
        if (is_array($_data)) {
            echo json_encode($_data);
        } else {
            echo $_data;
        }
        exit;
    }

    protected function getUid()
    {
        $this->_uid = isset($this->_gpc["uid"]) ? $this->_gpc["uid"] :
            KUYUAN_DEBUG ? 1 : $this->_w["member"]["uid"];
    }


    /**
     * 通过uid获取用户信息,如果微擎表里没有获取到用户的昵称和头像则手动获取设置微擎表的用户头像和昵称
     * @param $_uid
     * @return mixed
     */
    protected function uidToInfo($_uid)
    {
        $_member = Cache::get(Cache::MEMBER,array("uid"=>$_uid));
        if(!$_member || KUYUAN_DEBUG){
            $_member = $this->getMemberInfo($_uid);
            Cache::set(Cache::MEMBER,$_member,array("uid"=>$_uid));
        }
        return $_member;
    }

    /**
     * 通过uid获取用户信息,如果微擎表里没有获取到用户的昵称和头像则手动获取设置微擎表的用户头像和昵称
     * @param $_uid
     * @param array $_return
     * @param number $_count 循环次数
     * @return mixed
     */
    private function getMemberInfo($_uid,Array $_return = array('nickname','avatar'),$_count = 0)
    {
        if(!$this->WqMember instanceof WqMemberMod) $this->WqMember = new WqMemberMod();
        $_member = $this->WqMember->findOne(array('uid'=>$_uid),$_return);
        if($_count ==0){
            if((isset($_member['nickname']) && $_member['nickname'] =='') || (isset($_member['avatar']) && $_member['avatar'] == '')){
                $WxApi = \WeAccount::create();
                $_fans = $this->uidToFans($_uid);
                $_wx_member = $WxApi->fansQueryInfo($_fans['openid']);
                $this->WqMember->update(array('nickname'=>$_wx_member['nickname'],'avatar'=>$_wx_member['headimgurl']),array('uid'=>$_uid));
                return $this->getMemberInfo($_uid,$_return,++$_count);
            }
        }
        return $_member;
    }

    /**
     * 通过uid获取用户的粉丝信息
     * @param $_uid
     * @param array $_return
     * @return
     */
    protected function uidToFans($_uid,Array $_return = array('openid'))
    {
        if(!$this->WqFans instanceof WqFansMod) $this->WqFans = new WqFansMod();
        return $this->WqFans->findOne(array('uid'=>$_uid),$_return);
    }

    //-----------红包相关------------------

    /**
     * 获取红包信息
     * @param int $_red_id 红包id
     * @return array data  红包数据
     */
    protected function getRedPackage($_red_id)
    {
        $_data = Cache::get(Cache::RED,array("red_id"=>$_red_id));
        if(!$_data || KUYUAN_DEBUG){
            $this->Red = new RedMod();
            $_return = array("id","uid","money","number","type","title","cover","share","charge","pay","red_type","media","open_check","time","area_type","area","link_id","coerce_phone");
            $_data = $this->Red->findOne(array("id"=>$_red_id),$_return);
            if($_data["area_type"] > 0){
                $_data["area"] = json_decode($_data["area"],true);
            }
            $_link = $this->redIdToLink($_data["link_id"]);
            if($_link) $_data["link"] = $_link;
            Cache::set(Cache::RED,$_data,array("red_id"=>$_red_id));
        }
        return $_data;
    }

    /**
     * 通过推广id返回红包的推广链接
     * @param $_link_id 推广链接id
     * @return Array 返回红包的推广链接
     */
    private function redIdToLink($_link_id)
    {
        $this->Link = new LinkMod();
        return $this->Link->findOne(array("id"=>$_link_id),array("name","link"));
    }

    /**
     * 通过红包id返回对应红包的类型
     * @param number red_id 红包id
     * @return string
     */
    protected function redIdToType($_red_id)
    {
        if(!$this->Red instanceof RedMod) $this->Red = new RedMod();
        $_red = $this->Red->findOne(array('id'=>$_red_id),array('red_type'));
        $_red_type = '普通红包';
        if($_red['red_type'] == 'pic'){
            $_red_type = '图片红包';
        }else if($_red['red_type'] == 'video'){
            $_red_type = '视频红包';
        }
        return $_red_type;
    }

    //---------二维码-------------

    /**
     * 获取，生成一个二维码
     * @param string $_dir_name 二维码存放的目录名字
     * @param string $_file_name 文件名
     * @param string $_url  二维码储存的链接
     * @param boolean $_build 是否重新生成
     * @return mixed
     */
    protected function getQrcode($_dir_name,$_file_name,$_url,$_build = false)
    {
        $_dir = ATTACHMENT_ROOT.'kuyuan_red/'.$this->_uniacid.'/'.$_dir_name.'/';
        $_filename = $_dir.$_file_name.'.png';
        $_path = 'kuyuan_red/'.$this->_uniacid.'/'.$_dir_name.'/'.$_file_name.'.png';
        if(is_file($_filename) && !$_build){
            return $this->_w['attachurl_local'].$_path;
        }else{
            if(!is_dir($_dir)){
                mkdir($_dir,0777,true);
            }
            $Qrcode = new QrCode($_url);
            $Qrcode->setSize(500);
            $Qrcode->save($_filename);
            return $this->_w['attachurl_local'].$_path;
        }
    }
}